package com.czy.controller;

import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Author chenzy
 * @Date 2020/10/9 16:26
 * @Version 1.0
 * @Description
 */
@Controller
@RequestMapping(value = "/redis")
public class RedissonController {



    @Autowired
    private RedissonClient redissonClient;


    @RequestMapping("/set")
    public boolean set( ) {
        RList<String> interests = redissonClient.getList("interests");
        interests.add("篮球");
        interests.add("爬山");
        interests.add("编程");

        RKeys rKeys = redissonClient.getKeys();
        System.out.println(rKeys);
        try {

        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
        }
        return true;
    }


    @RequestMapping("/lock")
    public boolean lock(Long productId, Integer productQuantity) {
        String key = "dec_store_lock_" + productId;
        RLock lock = redissonClient.getLock(key);
        try {
            //加锁 操作很类似Java的ReentrantLock机制
            lock.lock();
//            ProductInfo productInfo = productInfoMapper.selectByPrimaryKey(productId);
//如果库存为空
//            if (productInfo.getProductStock() == 0) {
//                return false;
//            }
            //简单减库存操作 没有重新写其他接口了
// productInfo.setProductStock(productInfo.getProductStock() - 1);
//            productInfoMapper.updateByPrimaryKey(productInfo);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            //释放锁
            lock.unlock();
        }
        return true;
    }




}
